var express = require('express');
var router = express.Router();
var UserModel = require('../models/users');
var nowData = require('./common').nowDate();
var redisModel = require('../lib/redis');

router.get('/', function (req, res, next) {
    res.render('signin', {title: 'shopping'})
});
router.post('/', function (req, res, next) {
    redisModel.getRedisLen(req.body.name, function (err, data) {
        console.log(data);
        if (parseInt(data) < 1) {
            UserModel.getName(req.body.name)
                .then(function (result) {
                    if (result) {
                        if (result.password == req.body.password) {
                            req.session._id = result._id;
                            req.session.name = req.body.name;
                            req.session.admin = result.admin;
                            redisModel.setRedis(result.name, req.sessionID, result._id, function (err, data) {
                                if (err) {
                                    return res.json({status: 500, message: 'redis出错'});
                                }
                                return res.json({status: 200, message: '登录成功'});
                            })
                        } else {
                            return res.json({status: 400, message: '密码出错'});
                        }
                    } else {
                        return res.json({status: 400, message: '用户名不存在'});
                    }
                })
                .catch(function (e) {
                    next(e);
                })
        } else {
            return res.json({status: 500, message: "该用户已经登录"});
        }
    });

});

router.get('/signout', function (req, res, next) {
    redisModel.delRedis(req.session.name, req.sessionID, function (err, data) {
        if (err) {
            return res.json({status: 500, message: '退出失败'});
        } else {
            req.session.name = null;
            req.session.admin = null;
            return res.redirect('/user')
        }
    });
});

router.get('/register', function (req, res, next) {
    res.render('register', {title: 'Express'})
});
router.post('/register', function (req, res, next) {
    console.log(req.body);
    var admin = 0;
    if (req.body.admin == '店主') {
        admin = 2;
    }
    var userInfo = {
        name: req.body.name,
        password: req.body.password,
        photo: req.body.photo,
        idCard: req.body.idCard,
        SA: req.body.SA,
        email: req.body.email,
        createdAt: nowData,
        admin: admin
    };
    UserModel.create(userInfo)
        .then(function (result) {
            console.log(result);
            user = result.ops[0];
            req.session.user = user;
            return res.json({status: 200, message: '注册成功'})
        })
        .catch(function (e) {
            next(e);
        });
});
router.get('/name', function (req, res, next) {
    UserModel.getName(req.query.name)
        .then(function (user) {
            if (!user) {
                return res.json({valid: true})
            } else {
                return res.json({valid: false})
            }
        })
        .catch(next);
});
router.get('/email', function (req, res, next) {
    UserModel.getEmail(req.query.email)
        .then(function (user) {
            if (!user) {
                return res.json({valid: true})
            } else {
                return res.json({valid: false})
            }
        })
        .catch(next);
});
router.get('/photo', function (req, res, next) {
    UserModel.getPhoto(req.query.photo)
        .then(function (user) {
            if (!user) {
                return res.json({valid: true})
            } else {
                return res.json({valid: false})
            }
        })
        .catch(next);
});
module.exports = router;
